From 27158189860fe721bf206e54325c3b536d36082c Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Tue, 28 Jul 2009 21:13:48 +0000 Subject: [PATCH] * Added fields to list=search output: size, wordcount, timestamp, snippet * Where supported by backend, list=search adds a 'searchinfo' element with optional info: 'totalhits' count and 'suggestion' alternate query term Snippets added to result items earlier by Roan; extended this with the other byte size, word count, and timestamp available on the result items and exposed through the regular UI. Had to work out a backwards-compatible method for the search meta-information with Roan; added a second 'searchinfo' element since adding attributes to 'search' would break compatibility for JSON output (despite being safe in XML). 'searchinfo' is present only if the backend supports the extra info and has something available; 'totalhits' with a total hit count and 'suggestion' for an alternate query suggestion (exposed as "Did you mean X?" link in UI). Note that total hit counts can be enabled for MySQL backend now by setting the experimental option $wgSearchMySQLTotalHits, but did-you-mean suggestions are not yet supported and need to be tested with a hack or another backend. Sample XML and JSON output with the new searchinfo items (which can be present whether or not there are any result items): { "query": { "searchinfo": { "totalhits": 0, "suggestion": "joe momma" }, "search": [ ] } } The suggestion value is suitable for plugging back in as a search term, if present. --- RELEASE-NOTES | 4 +++- includes/api/ApiQuerySearch.php | 12 ++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index c730500a40..b0fd39d530 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -384,7 +384,6 @@ this. Was used when mwEmbed was going to be an extension. * (bug 18720) Add anchor field to action=parse&prop=sections output * (bug 19423) The initial file description page used caption in user lang rather than UI lang -* Added snippet field to list=search output * (bug 17809) Add number of users in user groups to meta=siteinfo * (bug 18533) Add readonly reason to readonly exception * (bug 19528) Added XSLT parameter to API queries in format=xml @@ -392,6 +391,9 @@ this. Was used when mwEmbed was going to be an extension. parameter in action=edit * (bug 19090) Added watchlist parameter, deprecated watch and unwatch parameter in action=edit +* Added fields to list=search output: size, wordcount, timestamp, snippet +* Where supported by backend, list=search adds a 'searchinfo' element with + optional info: 'totalhits' count and 'suggestion' alternate query term === Languages updated in 1.16 === diff --git a/includes/api/ApiQuerySearch.php b/includes/api/ApiQuerySearch.php index 4a3b892506..1d1a44f2b6 100644 --- a/includes/api/ApiQuerySearch.php +++ b/includes/api/ApiQuerySearch.php @@ -86,6 +86,15 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { if (is_null($matches)) $this->dieUsage("{$what} search is disabled", "search-{$what}-disabled"); + + $totalhits = $matches->getTotalHits(); + if( $totalhits !== null ) { + $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'totalhits', $totalhits ); + } + if( $matches->hasSuggestion() ) { + $this->getResult()->addValue( array( 'query', 'searchinfo' ), 'suggestion', + $matches->getSuggestionQuery() ); + } $terms = $wgContLang->convertForSearchResult($matches->termMatches()); $titles = array (); @@ -106,6 +115,9 @@ class ApiQuerySearch extends ApiQueryGeneratorBase { $vals = array(); ApiQueryBase::addTitleInfo($vals, $title); $vals['snippet'] = $result->getTextSnippet($terms); + $vals['size'] = $result->getByteSize(); + $vals['wordcount'] = $result->getWordCount(); + $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $result->getTimestamp() ); $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals); if(!$fit) { -- 2.20.1